<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <div th:replace="./widget/base"></div>
</head>
<body>

<div id="mailToolbar">
    <a th:if="${#authorization.expression('hasRole('''+ SYSTEM_ADMIN +''') OR hasAuthority(''/COMPONENT/MAIL/MAIL_EDIT_AND_SEND'')')}"
       href="javascript:" class="easyui-linkbutton" iconCls="fa fa-reply" plain="true" onclick="mailEditAndSendDialog();">编辑重发</a>

    <a th:if="${#authorization.expression('hasRole('''+ SYSTEM_ADMIN +''') OR hasAuthority(''/COMPONENT/MAIL/MAIL_DELETE_ALL'')')}"
       href="javascript:" class="easyui-linkbutton" iconCls="fa fa-close" plain="true"
       onclick="mailDeleteAll();">删除</a>

    <a th:if="${#authorization.expression('hasRole('''+ SYSTEM_ADMIN +''') OR hasAuthority(''/COMPONENT/MAIL/MAIL_RELOAD'')')}"
       href="javascript:" class="easyui-linkbutton" iconCls="fa fa-refresh" plain="true" onclick="mailReload();">刷新</a>
</div>

<table id="mail"></table>

<script type="text/javascript">

    $(function () {
        var SMS_STATUS = [];
        expressui.ajax({
            url: Api.System.DICTIONARY + '/list_all_by_key/SMS_STATUS',
            async: false,
            success: function (data) {
                if (data) {
                    SMS_STATUS = data;
                }
            }
        });

        var IS_OR_NOT = [];
        expressui.ajax({
            url: Api.System.DICTIONARY + '/list_all_by_key/IS_OR_NOT',
            async: false,
            success: function (data) {
                if (data) {
                    IS_OR_NOT = data;
                }
            }
        });

        // 邮件发送队列
        var columns = [
            {field: 'checkbox', checkbox: true},
            {field: 'id', title: 'ID'},
            {
                field: 'status', title: '状态', formatter: function (value, row, index) {
                    return expressui.grid.formatter(value, SMS_STATUS);
                }
            },
            {field: 'from', title: '发件人'},
            {field: 'to', title: '收件人'},
            {field: 'subject', title: '主题'},
            {field: 'text', title: '内容'},
            {
                field: 'html', title: '是否为 HTML', formatter: function (value, row, index) {
                    return expressui.grid.formatter(value, IS_OR_NOT);
                }
            },
            {field: 'error', title: '发信报错'},
            {field: 'remark', title: '备注'},
            {field: 'gmtModified', title: '更新时间'},
            {field: 'gmtCreated', title: '创建时间'}
        ];

        $('#mail').datagrid(expressui.grid.create, {
            title: '邮件队列',
            url: Api.MAIL + '/page_all',
            columns: [columns]
        });
    });

    function mailEditAndSendDialog() {
        var one = $('#mail').datagrid(expressui.grid.getCheckedOneShowAlert);
        if (one) {
            $('#mailEditAndSendDialog').dialog(expressui.dialog.create, {
                title: '编辑重发',
                grid: {type: 'datagrid', selector: '#mail'},
                href: Page.MAIL + '/edit_and_send_dialog',
                get: {url: Api.MAIL + '/one/{id}', method: 'get'},
                save: {url: Api.MAIL + '/add_one', method: 'post'},
                buttons: [{
                    text: '发送',
                    iconCls: 'fa fa-save',
                    handler: expressui.dialog.save,
                    reload: [{type: 'datagrid', selector: '#mail'}],
                    success: '操作成功'
                }, {
                    text: '关闭',
                    iconCls: 'fa fa-close',
                    handler: expressui.dialog.close
                }]
            });
        }
    }

    function mailReload() {
        expressui.grid.reload({type: 'datagrid', selector: '#mail'})
    }

    function mailDeleteAll() {
        $('#mail').datagrid(expressui.grid.deleteRows, {
            url: Api.MAIL + '/delete_all',
            method: 'post',
            success: '删除成功',
            reload: [{type: 'datagrid', selector: '#mail'}],
        });
    }
</script>

</body>
</html>